package handler

import (
	"context"
	"fmt"
	"server/basic/global"
	"server/models"
	__ "server/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Prose(_ context.Context, in *__.ProseRequest) (*__.ProseResponse, error) {
	var user models.Flowers
	if user.Answer == "管理员" {
		add := models.Flowers{
			ExamTypes: in.ExamTypes,
			Tirdy:     in.Tirdy,
			EaxmOne:   in.EaxmOne,
			ExamTwo:   in.ExamTwo,
			Question:  in.Question,
			Answer:    in.Answer,
		}
		err := global.DB.Create(&add).Error
		if err != nil {
			return nil, fmt.Errorf("试题创建失败")
		}
		if user.Answer == "普通员工" && user.ExamTypes == "已通过" {
			adds := models.Flowers{
				ExamTypes: in.ExamTypes,
				Tirdy:     in.Tirdy,
				EaxmOne:   in.EaxmOne,
				ExamTwo:   in.ExamTwo,
				Question:  in.Question,
				Answer:    in.Answer,
			}
			err = global.DB.Create(&adds).Error
			if err != nil {
				return nil, fmt.Errorf("试题创建失败")
			}
		}
	}

	return &__.ProseResponse{
		Id: int64(user.Id),
	}, nil
}
func (s *Server) Check(_ context.Context, in *__.CheckRequest) (*__.CheckResponse, error) {

	var find []models.Flowers
	global.DB.Find(&find)
	var finds []*__.List
	for _, v := range find {
		show := &__.List{
			CheckPeople: v.CheckPeople,
			CheckNum:    string(v.CheckNum),
			EaxmOne:     v.EaxmOne,
			ExamTwo:     v.ExamTwo,
			CreateNum:   string(v.CreateNum),
			Status:      v.Status,
			Question:    v.Question,
		}
		finds = append(finds, show)
	}

	return &__.CheckResponse{
		List: finds,
	}, nil
}
func (s *Server) Info(_ context.Context, in *__.InfoRequest) (*__.InfoResponse, error) {

	find := models.Flowers{
		Id: int32(in.Id),
	}
	global.DB.First(&find)

	return &__.InfoResponse{
		CheckPeople: find.CheckPeople,
		CheckNum:    string(find.CheckNum),
		EaxmOne:     find.EaxmOne,
		ExamTwo:     find.ExamTwo,
		CreateNum:   string(find.CreateNum),
		Status:      find.Status,
		Question:    find.Question,
	}, nil
}
func (s *Server) Remove(_ context.Context, in *__.RemoveRequest) (*__.RemoveResponse, error) {

	find := models.Flowers{
		Id: int32(in.Id),
	}
	global.DB.Delete(&find)

	return &__.RemoveResponse{}, nil
}
